package com.mobile.collect.activitys;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.mobile.collect.R;
import com.mobile.collect.present.LoginPresent;
import com.mobile.collect.utils.AppContents;
import com.mobile.collect.view.MyEditText;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.mvp.XActivity;

public class LoginActivity extends XActivity<LoginPresent> {

    @BindView(R.id.nameEdit)
    MyEditText nameEdit;
    @BindView(R.id.pwdEdit)
    MyEditText pwdEdit;


    @Override
    public int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public void getIntentData() {

    }

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("登录");
        leftIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }
        });


        String loginName = SharedPref.getInstance(this).getString(AppContents.LOGINNAME, "");
        String password = SharedPref.getInstance(this).getString(AppContents.PASSWORD, "");
        nameEdit.setValue(loginName);
        pwdEdit.setValue(password);
        if (AppContents.ISTEST) {
            nameEdit.setValue("user1");
            pwdEdit.setValue("123456");
        }
    }


    @Override
    public LoginPresent newP() {
        return new LoginPresent();
    }


    @OnClick(R.id.submitBtn)
    public void onViewClicked() {
        String idCard = nameEdit.getValue();
        String pwd = pwdEdit.getValue();
        showLoadDialog();
        getP().login(idCard, pwd);
    }

    public void loginSuccess() {
        disLoadDialog();
        SharedPref.getInstance(this).putString(AppContents.LOGINNAME, nameEdit.getValue());
        SharedPref.getInstance(this).putString(AppContents.PASSWORD, pwdEdit.getValue());
        finish();
    }
    public static void lunch(Context context){
        context.startActivity(new Intent(context,LoginActivity.class));
    }
}
